package com.example.myapplication.part03.part15;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.myapplication.R;

import java.io.File;
import java.util.List;

public class ListViewAdapter extends BaseAdapter {
    private List<File> files;
    private LayoutInflater layoutInflater;

    public ListViewAdapter(List<File> files, LayoutInflater layoutInflater) {
        this.files = files;
        this.layoutInflater = layoutInflater;
    }

    @Override
    public int getCount() {
        return files.size();
    }

    @Override
    public Object getItem(int position) {
        return files.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HoldView holdView;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_listview, null);
            holdView = new HoldView(convertView);
            convertView.setTag(holdView);
        } else {
            holdView = (HoldView) convertView.getTag();
        }

        File file = files.get(position);
        holdView.getIv_con().setImageResource(R.drawable.f5_pic); // 替换为实际图片资源
        holdView.getTx_name().setText(file.getName());

        return convertView;
    }

    private class HoldView {
        ImageView iv_con;
        TextView tx_name;

        public HoldView(View view) {
            iv_con = view.findViewById(R.id.item_file_imageview);
            tx_name = view.findViewById(R.id.item_file_name);
        }

        public ImageView getIv_con() {
            return iv_con;
        }

        public TextView getTx_name() {
            return tx_name;
        }
    }
}